//
// Rectangle.hh for  in /home/gomes_m//workzone/test/gdl
// 
// Made by mickael gomes
// Login   <gomes_m@epitech.net>
// 
// Started on  Tue Apr 30 16:35:24 2013 mickael gomes
// Last update Thu May 30 11:25:24 2013 mickael gomes
//

#include	<cstdio>
#include	<cstdlib>
#include	<iostream>
#include	"Rectangle.hh"

void Rectangle::initialize(std::string const &path, float x1, float y1, float x2, float y2, float z)
{
  pos_x1_ = x1;
  pos_x2_ = x2;
  pos_y1_ = y1;
  pos_y2_ = y2;
  z_ = z;
  std::cout << "Cpoulet" << std::endl;
  texture_ = gdl::Image::load(path);
}

void Rectangle::initVertex()
{
  vertexX1_ = 0;
  vertexX2_ = 1;
  vertexY1_ = 0;
  vertexY2_ = 1;
}

void Rectangle::update(Vector3f const &coord)
{
  pos_x1_ = coord.posX1_;
  pos_x2_ = coord.posX2_;
  pos_y1_ = coord.posY1_;
  pos_y2_ = coord.posY2_;
  z_ = coord.posZ_;
  move_x_ = coord.moveX_;
  move_y_ = coord.moveY_;
}

void Rectangle::draw(void)
{
  texture_.bind();
  glBegin(GL_QUADS);
  glTexCoord2f(vertexX1_, vertexY1_);
  glVertex3f(pos_x1_, pos_y1_, z_);

  glTexCoord2f(vertexX1_, vertexY2_);
  glVertex3f(pos_x1_, pos_y2_, z_);

  glTexCoord2f(vertexX2_, vertexY2_);
  glVertex3f(pos_x2_, pos_y2_, z_);

  glTexCoord2f(vertexX2_, vertexY1_);
  glVertex3f(pos_x2_, pos_y1_, z_);
  glEnd();
  glDisable(GL_BLEND);
}

void Rectangle::draw(Vector3f const &coord)
{
  texture_.bind();
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glBegin(GL_QUADS);
  glTexCoord2f(0.0f, 0.0f);
  glVertex3f(coord.posX1_ + coord.moveX_, coord.posY1_ + coord.moveY_, coord.posZ_);

  glTexCoord2f(0.0f, 1.0f);
  glVertex3f(coord.posX1_ + coord.moveX_, coord.posY2_ + coord.moveY_, coord.posZ_);

  glTexCoord2f(1.0f, 1.0f);
  glVertex3f(coord.posX2_ + coord.moveX_, coord.posY2_ + coord.moveY_, coord.posZ_);

  glTexCoord2f(1.0f, 0.0f);
  glVertex3f(coord.posX2_ + coord.moveX_, coord.posY1_ + coord.moveY_, coord.posZ_);
  glEnd();
  glDisable(GL_BLEND);
}
